/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tracker.stub;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import tracker.entity.TorrentsEntity;
import tracker.entity.TorrentsEntityFacade;

/**
 * Workshop Class ID : SE0691 Student ID : SE90009 Student Name : Phạm Nguyễn
 * Nhật Trung Due Date : 12 November 2012 I declare that this assignment is my
 * own work in accordance with FPT Policy.
 */
public class TorrentsSessionStub {

    TorrentsEntityFacade torrentsEntityFacade = lookupTorrentsEntityFacadeBean();

    public void insert(TorrentsEntity entity) {
        torrentsEntityFacade.create(entity);
    }

    public TorrentsEntity findUserById(int userId) {
        return torrentsEntityFacade.find(userId);
    }

    public void deleteById(int userId) {
        TorrentsEntity entity = findUserById(userId);
        torrentsEntityFacade.remove(entity);
    }

    public void change(TorrentsEntity entity) {
        torrentsEntityFacade.edit(entity);
    }

    public List<TorrentsEntity> findAll() {
        return torrentsEntityFacade.findAll();
    }

    public boolean checkExistedTorrentName(String torrentName) {
        return torrentsEntityFacade.checkIsExistedTorrent(torrentName);
    }

    public List findAllTorrentsByTorrentName(String torrentName){
        return torrentsEntityFacade.findAllTorrentsByTorrentName(torrentName);
    }
    
    private TorrentsEntityFacade lookupTorrentsEntityFacadeBean() {
        try {
            Context c = new InitialContext();
            return (TorrentsEntityFacade) c.lookup("java:global/ASG2-Tracker1/ASG2-Tracker1-ejb/TorrentsEntityFacade!tracker.entity.TorrentsEntityFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
